package com.oazon.moco.playlist.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.oazon.moco.playlist.bean.ItemType;
import com.oazon.moco.playlist.bean.MediaMetadataBean;

public class MediaMetadataDao {
	
	private SQLiteDatabase db;
	
	private static final String TABLE_NAME = "media_metadata";

	private static final String ID = "id";
	private static final String ITEM_TYPE = "item_type";
	private static final String TITLE = "title";
	private static final String MIME_TYPE = "mime_type";
	private static final String CREAT_TIME ="creat_time";
	private static final String LIFECYCLE ="lifecycle";
	private static final String PARENT_ID ="parent_id";
	private static final String PROVIDER ="provider";
	
	
	private static final String CURRENT_HOST = "currentHost";;
	private static final String CURRENT_SHOW_ID = "current_show_id";
	private static final String CURRENT_SHOW = "current_show";
	private static final String SECONDS_REMAINING = "seconds_remaining";
	private static final String SECONDS_TO_NEXT_SHOW = "seconds_to_next_show";
	private static final String BITRATE = "bitrate";
	private static final String LOGO = "logo";
    
	private static final String ARTIST = "artist";
	private static final String ARTIST_ID = "artist_id";
	private static final String COMPOSER_ID = "composer_id";
	private static final String COMPOSER = "composer";
	private static final String ALBUM_ID = "album_id";
	private static final String ALBUM = "album";
	private static final String ALBUM_ARTIST_ID = "album_artist_id";
	private static final String ALBUM_ARTIST = "albumArtist";
	private static final String GENRE_ID = "genre_id";
	private static final String GENRE = "genre";
	private static final String DURATION = "duration";
	private static final String RATING = "rating";
	private static final String ALBUM_ART_URI = "album_art_uri";
	private static final String CAN_PLAY = "can_play";
	private static final String CAN_SKIP = "can_skip";
	private static final String CAN_ADD_TO_FAVORITES = "can_add_to_favorites";
	
	public MediaMetadataDao(Context context) {
		MocoDB resourceDB = new MocoDB(context);
		db = resourceDB.getWritableDatabase();
	}
	
	public long insertIntomediaMetadata(MediaMetadataBean bean){
		if(bean == null){
			return -1l;
		}
		String time = new Date().toString();
		bean.setCreatTime(time);
		
		ContentValues values = new ContentValues();

		values.put(ID, bean.getId());
		deleteRepeatById(bean.getId());
		
		if(bean.getItemType() != null){
			values.put(ITEM_TYPE, bean.getItemType().value());	
		}
		values.put(TITLE, bean.getTitle());
		values.put(MIME_TYPE, bean.getMimeType());
		values.put(LIFECYCLE, bean.getLifecycle());
		values.put(PARENT_ID, bean.getParentId());
		values.put(CREAT_TIME, bean.getCreatTime());
		values.put(PROVIDER, bean.getProvider());
		
		if (bean.getStreamMetadata() != null) {
			values.put(CURRENT_HOST, bean.getStreamMetadata().getCurrentHost());
			values.put(CURRENT_SHOW_ID, bean.getStreamMetadata().getCurrentShowId());
			values.put(CURRENT_SHOW, bean.getStreamMetadata().getCurrentShow());
			values.put(SECONDS_REMAINING, bean.getStreamMetadata().getSecondsRemaining());
			values.put(SECONDS_TO_NEXT_SHOW, bean.getStreamMetadata().getSecondsToNextShow());
			values.put(BITRATE, bean.getStreamMetadata().getBitrate());
			values.put(LOGO, bean.getStreamMetadata().getLogo());	
		}

		
		if (bean.getTrackMetadata() != null) {
			values.put(ARTIST, bean.getTrackMetadata().getArtist());
			values.put(ARTIST_ID, bean.getTrackMetadata().getArtistId());
			values.put(COMPOSER_ID, bean.getTrackMetadata().getComposerId());
			values.put(COMPOSER, bean.getTrackMetadata().getComposer());
			values.put(ALBUM_ID, bean.getTrackMetadata().getAlbumId());
			values.put(ALBUM, bean.getTrackMetadata().getAlbum());
			values.put(ALBUM_ARTIST_ID, bean.getTrackMetadata().getAlbumArtistId());
			values.put(ALBUM_ARTIST, bean.getTrackMetadata().getAlbumArtist());
			values.put(GENRE_ID, bean.getTrackMetadata().getGenreId());
			values.put(GENRE, bean.getTrackMetadata().getGenre());
			values.put(DURATION, bean.getTrackMetadata().getDuration());
			values.put(RATING, bean.getTrackMetadata().getRating());
			values.put(ALBUM_ART_URI, bean.getTrackMetadata().getAlbumArtURI());
			values.put(CAN_PLAY, bean.getTrackMetadata().isCanPlay());
			values.put(CAN_SKIP, bean.getTrackMetadata().isCanSkip());
			values.put(CAN_ADD_TO_FAVORITES, bean.getTrackMetadata().isCanAddToFavorites());
		}
		

		return db.insert(TABLE_NAME, null, values);
		
	}
	
	private long deleteRepeatById(String id) {
		if (id == null || id.equals(""))
			return -1;
		
		return db.delete(TABLE_NAME, ID+"=?", new String[]{ id });
	}
	
	public long insertListMediaMetadata(List<MediaMetadataBean> mediaMetadataBeans){
		android.util.Log.d("wowdd1", "insertListMediaMetadata");
		long size=0l;
		if(mediaMetadataBeans == null || mediaMetadataBeans.size()==0){
			return -1l;
		}
		for (MediaMetadataBean mediaMetadataBean : mediaMetadataBeans) {
			try {
				insertIntomediaMetadata(mediaMetadataBean);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			size ++;
		}
		android.util.Log.d("wowdd1", "insertListMediaMetadata " + mediaMetadataBeans.size());
		return size;
	}

	public List<MediaMetadataBean> selectByParentId(String parentId,String provider){
		
		Cursor cursor = db.query(TABLE_NAME, null, PARENT_ID+"=? and "+PROVIDER+"=?", new String[]{parentId,provider}, null, null, null);
		List<MediaMetadataBean> mediaMetadataBeans = new ArrayList<MediaMetadataBean>();
		MediaMetadataBean mediaMetadataBean = null;
		
		while (cursor.moveToNext()) {
			mediaMetadataBean = new MediaMetadataBean();
			mediaMetadataBean.setId(cursor.getString(cursor.getColumnIndex(ID)));
			mediaMetadataBean.setItemType(ItemType.fromValue(cursor.getString(cursor.getColumnIndex(ITEM_TYPE))));
			mediaMetadataBean.setTitle(cursor.getString(cursor.getColumnIndex(TITLE)));
			mediaMetadataBean.setMimeType(cursor.getString(cursor.getColumnIndex(MIME_TYPE)));
            mediaMetadataBean.setLifecycle(cursor.getString(cursor.getColumnIndex(LIFECYCLE)));
			mediaMetadataBean.setParentId(cursor.getString(cursor.getColumnIndex(PARENT_ID)));
            mediaMetadataBean.setCreatTime(cursor.getString(cursor.getColumnIndex(CREAT_TIME)));
			mediaMetadataBean.setProvider(cursor.getString(cursor.getColumnIndex(PROVIDER)));
			
			if (mediaMetadataBean.getStreamMetadata() != null) {
				mediaMetadataBean.getStreamMetadata().setCurrentHost(cursor.getString(cursor.getColumnIndex(CURRENT_HOST)));
				mediaMetadataBean.getStreamMetadata().setCurrentShowId(cursor.getString(cursor.getColumnIndex(CURRENT_SHOW_ID)));
				mediaMetadataBean.getStreamMetadata().setCurrentShow(cursor.getString(cursor.getColumnIndex(CURRENT_SHOW)));
				if (cursor.getString(cursor.getColumnIndex(SECONDS_REMAINING)) != null)
				    mediaMetadataBean.getStreamMetadata().setSecondsRemaining(Integer.parseInt(cursor.getString(cursor.getColumnIndex(SECONDS_REMAINING))));
				if (cursor.getString(cursor.getColumnIndex(SECONDS_TO_NEXT_SHOW)) != null)
				    mediaMetadataBean.getStreamMetadata().setSecondsToNextShow(Integer.parseInt(cursor.getString(cursor.getColumnIndex(SECONDS_TO_NEXT_SHOW))));
				if (cursor.getString(cursor.getColumnIndex(BITRATE)) != null)
				    mediaMetadataBean.getStreamMetadata().setBitrate(Integer.parseInt(cursor.getString(cursor.getColumnIndex(BITRATE))));
				mediaMetadataBean.getStreamMetadata().setLogo(cursor.getString(cursor.getColumnIndex(LOGO)));
			}

			if (mediaMetadataBean.getTrackMetadata() != null) {
				mediaMetadataBean.getTrackMetadata().setArtist(cursor.getString(cursor.getColumnIndex(ARTIST)));
				mediaMetadataBean.getTrackMetadata().setArtistId(cursor.getString(cursor.getColumnIndex(ARTIST_ID)));
				mediaMetadataBean.getTrackMetadata().setComposerId(cursor.getString(cursor.getColumnIndex(COMPOSER_ID)));
				mediaMetadataBean.getTrackMetadata().setComposer(cursor.getString(cursor.getColumnIndex(COMPOSER)));
				mediaMetadataBean.getTrackMetadata().setAlbumId(cursor.getString(cursor.getColumnIndex(ALBUM_ID)));
				mediaMetadataBean.getTrackMetadata().setAlbum(cursor.getString(cursor.getColumnIndex(ALBUM)));
				mediaMetadataBean.getTrackMetadata().setAlbumArtistId(cursor.getString(cursor.getColumnIndex(ALBUM_ARTIST_ID)));
				mediaMetadataBean.getTrackMetadata().setAlbumArtist(cursor.getString(cursor.getColumnIndex(ALBUM_ARTIST)));
				mediaMetadataBean.getTrackMetadata().setGenreId(cursor.getString(cursor.getColumnIndex(GENRE_ID)));
				mediaMetadataBean.getTrackMetadata().setGenre(cursor.getString(cursor.getColumnIndex(GENRE)));
				if (cursor.getString(cursor.getColumnIndex(DURATION)) != null)
				    mediaMetadataBean.getTrackMetadata().setDuration(Integer.parseInt(cursor.getString(cursor.getColumnIndex(DURATION))));
				if (cursor.getString(cursor.getColumnIndex(RATING)) != null)
				    mediaMetadataBean.getTrackMetadata().setRating(Integer.parseInt(cursor.getString(cursor.getColumnIndex(RATING))));
				mediaMetadataBean.getTrackMetadata().setAlbumArtURI(cursor.getString(cursor.getColumnIndex(ALBUM_ART_URI)));
				if (cursor.getString(cursor.getColumnIndex(CAN_PLAY)) != null)
				    mediaMetadataBean.getTrackMetadata().setCanPlay(Boolean.valueOf(cursor.getString(cursor.getColumnIndex(CAN_PLAY))));
				if (cursor.getString(cursor.getColumnIndex(CAN_SKIP)) != null)
				    mediaMetadataBean.getTrackMetadata().setCanSkip(Boolean.valueOf(cursor.getString(cursor.getColumnIndex(CAN_SKIP))));
				if (cursor.getString(cursor.getColumnIndex(CAN_ADD_TO_FAVORITES)) != null)
				    mediaMetadataBean.getTrackMetadata().setCanAddToFavorites(Boolean.valueOf(cursor.getString(cursor.getColumnIndex(CAN_ADD_TO_FAVORITES))));
			}


			mediaMetadataBeans.add(mediaMetadataBean);
		}
		

		return mediaMetadataBeans;
	}
	
	public int deleteByParentId(String parentId,String provider){
		if(parentId == null){
			return-1;
		}
		if(provider == null){
			provider ="";
		}
		return db.delete(TABLE_NAME, PARENT_ID+"=? and "+PROVIDER+"=?", new String[]{parentId,provider});
	}
	
	public synchronized int deleteByProvider(String provider) {
		if (provider == null) {
			provider = "";
		}
		return db
				.delete(TABLE_NAME, PROVIDER + "=?", new String[] { provider });
	}
}
